//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "EndCriteria.h"
using namespace Cephei::QL::Math::Optimization;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Math::Optimization::CEndCriteria::CEndCriteria (UInt64 maxIterations, UInt64 maxStationaryStateIterations, Double rootEpsilon, Double functionEpsilon, Double gradientNormEpsilon) 
{
	_pSpinlock = new boost::detail::spinlock ();
    try
    {
#ifdef HANDLE
        _phEndCriteria = NULL;
#endif
        QuantLib::Size _maxIterations = (QuantLib::Size)ValueHelper::Convert (maxIterations);
        QuantLib::Size _maxStationaryStateIterations = (QuantLib::Size)ValueHelper::Convert (maxStationaryStateIterations);
        QuantLib::Real _rootEpsilon = (QuantLib::Real)ValueHelper::Convert (rootEpsilon);
        QuantLib::Real _functionEpsilon = (QuantLib::Real)ValueHelper::Convert (functionEpsilon);
        QuantLib::Real _gradientNormEpsilon = (QuantLib::Real)ValueHelper::Convert (gradientNormEpsilon);
        _ppEndCriteria = new boost::shared_ptr<QuantLib::EndCriteria> (new QuantLib::EndCriteria ( _maxIterations,  _maxStationaryStateIterations,  _rootEpsilon,  _functionEpsilon,  _gradientNormEpsilon ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Math::Optimization::CEndCriteria::CEndCriteria (boost::shared_ptr<QuantLib::EndCriteria>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phEndCriteria = NULL;
#endif
	_ppEndCriteria = &childNative;
    
}
Cephei::QL::Math::Optimization::CEndCriteria::CEndCriteria (QuantLib::EndCriteria& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phEndCriteria = NULL;
#endif
	_ppEndCriteria = new boost::shared_ptr<QuantLib::EndCriteria> (&childNative);
    
    _EndCriteriaOwner = owner;
    
}

Cephei::QL::Math::Optimization::CEndCriteria::CEndCriteria (CEndCriteria^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phEndCriteria = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppEndCriteria = new boost::shared_ptr<QuantLib::EndCriteria> (copy->GetShared());
        
    }
}
Cephei::QL::Math::Optimization::CEndCriteria::CEndCriteria (System::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phEndCriteria = NULL;
#endif
	if (!t->IsSubclassOf(CEndCriteria::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Math::Optimization::CEndCriteria::CEndCriteria (QuantLib::Handle<QuantLib::EndCriteria>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phEndCriteria = &childNative;
	_ppEndCriteria = &static_cast<boost::shared_ptr<QuantLib::EndCriteria>>(childNative.currentLink());
    
    _EndCriteriaOwner = owner;
}
Cephei::QL::Math::Optimization::CEndCriteria::CEndCriteria (QuantLib::Handle<QuantLib::EndCriteria> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phEndCriteria = &childNative;
	_ppEndCriteria = &static_cast<boost::shared_ptr<QuantLib::EndCriteria>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Math::Optimization::CEndCriteria::CEndCriteria (QuantLib::EndCriteria childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phEndCriteria = NULL;
#endif
	_ppEndCriteria = new boost::shared_ptr<QuantLib::EndCriteria> (new QuantLib::EndCriteria (childNative));
    
}
#endif

Cephei::QL::Math::Optimization::CEndCriteria::~CEndCriteria ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppEndCriteria != NULL)
    {
	    delete _ppEndCriteria;
        _ppEndCriteria = NULL;
    }
}
Cephei::QL::Math::Optimization::CEndCriteria::!CEndCriteria ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppEndCriteria != NULL)
    {
	    delete _ppEndCriteria;
    }
}
QuantLib::EndCriteria& Cephei::QL::Math::Optimization::CEndCriteria::GetReference ()
{
    if (_ppEndCriteria == NULL) throw gcnew NativeNullException ();
	return **_ppEndCriteria;
}
boost::shared_ptr<QuantLib::EndCriteria>& Cephei::QL::Math::Optimization::CEndCriteria::GetShared ()
{
    if (_ppEndCriteria == NULL) throw gcnew NativeNullException ();
	return *_ppEndCriteria;
}
QuantLib::EndCriteria* Cephei::QL::Math::Optimization::CEndCriteria::GetPointer ()
{
    if (_ppEndCriteria == NULL) throw gcnew NativeNullException ();
	return &**_ppEndCriteria;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::EndCriteria>& Cephei::QL::Math::Optimization::CEndCriteria::GetHandle ()
{
	if (_phEndCriteria == NULL)
	{
		_phEndCriteria = new Handle<QuantLib::EndCriteria> (*_ppEndCriteria);
	}
	return *_phEndCriteria;
}
#endif
bool Cephei::QL::Math::Optimization::CEndCriteria::HasNative () 
{
	return (_ppEndCriteria != NULL);
}

Boolean Cephei::QL::Math::Optimization::CEndCriteria::CheckMaxIterations (UInt64 iteration, QL::Math::Optimization::EndCriteria::TypeEnum ecType)
{
    try
    {
        QuantLib::Size _iteration = (QuantLib::Size)ValueHelper::Convert (iteration);
        QuantLib::EndCriteria::Type _ecType = (QuantLib::EndCriteria::Type)ecType ;
    	bool _rv = (bool)(*_ppEndCriteria)->checkMaxIterations ( _iteration,  _ecType );   
        Boolean _nrv = (Boolean)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Boolean Cephei::QL::Math::Optimization::CEndCriteria::CheckStationaryFunctionAccuracy (Double f, Boolean positiveOptimization, QL::Math::Optimization::EndCriteria::TypeEnum ecType)
{
    try
    {
        QuantLib::Real _f = (QuantLib::Real)ValueHelper::Convert (f);
        bool _positiveOptimization = (bool)ValueHelper::Convert (positiveOptimization);
        QuantLib::EndCriteria::Type _ecType = (QuantLib::EndCriteria::Type)ecType ;
    	bool _rv = (bool)(*_ppEndCriteria)->checkStationaryFunctionAccuracy ( _f,  _positiveOptimization,  _ecType );   
        Boolean _nrv = (Boolean)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Boolean Cephei::QL::Math::Optimization::CEndCriteria::CheckStationaryFunctionValue (Double fxOld, Double fxNew, UInt64 statStateIterations, QL::Math::Optimization::EndCriteria::TypeEnum ecType)
{
    try
    {
        QuantLib::Real _fxOld = (QuantLib::Real)ValueHelper::Convert (fxOld);
        QuantLib::Real _fxNew = (QuantLib::Real)ValueHelper::Convert (fxNew);
        QuantLib::Size _statStateIterations = (QuantLib::Size)ValueHelper::Convert (statStateIterations);
        QuantLib::EndCriteria::Type _ecType = (QuantLib::EndCriteria::Type)ecType ;
    	bool _rv = (bool)(*_ppEndCriteria)->checkStationaryFunctionValue ( _fxOld,  _fxNew,  _statStateIterations,  _ecType );   
        Boolean _nrv = (Boolean)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Boolean Cephei::QL::Math::Optimization::CEndCriteria::CheckStationaryPoint (Double xOld, Double xNew, UInt64 statStateIterations, QL::Math::Optimization::EndCriteria::TypeEnum ecType)
{
    try
    {
        QuantLib::Real _xOld = (QuantLib::Real)ValueHelper::Convert (xOld);
        QuantLib::Real _xNew = (QuantLib::Real)ValueHelper::Convert (xNew);
        QuantLib::Size _statStateIterations = (QuantLib::Size)ValueHelper::Convert (statStateIterations);
        QuantLib::EndCriteria::Type _ecType = (QuantLib::EndCriteria::Type)ecType ;
    	bool _rv = (bool)(*_ppEndCriteria)->checkStationaryPoint ( _xOld,  _xNew,  _statStateIterations,  _ecType );   
        Boolean _nrv = (Boolean)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Boolean Cephei::QL::Math::Optimization::CEndCriteria::CheckZeroGradientNorm (Double gNorm, QL::Math::Optimization::EndCriteria::TypeEnum ecType)
{
    try
    {
        QuantLib::Real _gNorm = (QuantLib::Real)ValueHelper::Convert (gNorm);
        QuantLib::EndCriteria::Type _ecType = (QuantLib::EndCriteria::Type)ecType ;
    	bool _rv = (bool)(*_ppEndCriteria)->checkZeroGradientNorm ( _gNorm,  _ecType );   
        Boolean _nrv = (Boolean)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Math::Optimization::CEndCriteria::FunctionEpsilon::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppEndCriteria)->functionEpsilon ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Math::Optimization::CEndCriteria::GradientNormEpsilon::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppEndCriteria)->gradientNormEpsilon ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
UInt64 Cephei::QL::Math::Optimization::CEndCriteria::MaxIterations::get ()
{
    try
    {
    	QuantLib::Size _rv = (QuantLib::Size)(*_ppEndCriteria)->maxIterations ( );   
        UInt64 _nrv = (UInt64)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
UInt64 Cephei::QL::Math::Optimization::CEndCriteria::MaxStationaryStateIterations::get ()
{
    try
    {
    	QuantLib::Size _rv = (QuantLib::Size)(*_ppEndCriteria)->maxStationaryStateIterations ( );   
        UInt64 _nrv = (UInt64)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Math::Optimization::CEndCriteria::RootEpsilon::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppEndCriteria)->rootEpsilon ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Math::Optimization::IEndCriteria^ Cephei::QL::Math::Optimization::CEndCriteria_Factory::Create (UInt64 maxIterations, UInt64 maxStationaryStateIterations, Double rootEpsilon, Double functionEpsilon, Double gradientNormEpsilon)
{
    return gcnew CEndCriteria ( maxIterations,  maxStationaryStateIterations,  rootEpsilon,  functionEpsilon,  gradientNormEpsilon);
}
